﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WindowsFormsApplication1
{
    public partial class DD : UserControl
    {
        public DD()
        {
            InitializeComponent();
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked)
            {
                textBox8.Enabled = false;
            }
            else
            {
                textBox8.Enabled = true;
            }
        }

        public List<string> GetList(string text)
        {
            string[] list1 = null;
            List<string> list2 = new List<string>(); ;
            if (checkBox1.Checked)
            {
                list1 = text.Split(Environment.NewLine.ToCharArray());

                for (int i = 0; i < list1.Count(); i++)
                {
                    if (i % 2 == 0)
                    {
                        if (string.IsNullOrEmpty(list1[i]))
                        {
                            list2.Add("");
                        }
                        else
                        {
                            list2.Add(list1[i]);
                        }
                    }
                }

            }
            else
            {
                char fenge = char.Parse(textBox8.Text);
                list1 = text.Split(fenge);
                foreach (string item in list1)
                {
                    if (!string.IsNullOrEmpty(item))
                    {
                        list2.Add(item);
                    }
                }
            }
            
            return list2;
        }

        public bool Yanzheng(int ListNumber,ref string errom)
        {
            bool err = false;
            
            foreach (Control item in panel1.Controls)
            {
                if (item is TextBox)
                {
                    TextBox TextBox = (TextBox)item;
                    List<string> list1 = GetList(item.Text);

                    errom += "列："+ TextBox.Name + "，数量："+ list1.Count() + "\r\n";
                    if (ListNumber != list1.Count())
                    {
                        err = true;
                    }
                }
            }

            return err;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                #region 获取第一个集合的数量
                int ListNumber = 0;
                foreach (Control item in panel1.Controls)
                {
                    if (item is TextBox && item.Name == "1")
                    {
                        ListNumber = GetList(item.Text).Count();
                        break;
                    }
                } 
                #endregion

                string errom = "";
                if (Yanzheng(ListNumber,ref errom))
                {
                    MessageBox.Show("集合数量不统一：\r\n" + errom);
                    return;
                }

                int number = int.Parse(textBox1.Text);
                string yuensuan = textBox2.Text;
                int zizeng = int.Parse(textBox7.Text);
                
                StringBuilder jieguo = new StringBuilder();
                if (yuensuan == "" || number == 0)
                {
                    return;
                }

                string dan = "";

                for (int i = 0; i < ListNumber; i++)
                {
                    dan = yuensuan;
                    foreach (Control item in panel1.Controls)
                    {
                        if (item is TextBox)
                        {
                            TextBox TextBox = (TextBox)item;

                            List<string> list1 = GetList(item.Text);
                            
                            dan = dan.Replace("[val" + item.Name + "]", list1[i]);
                            
                        }
                    }
                    
                    dan = dan.Replace("[zint]", (zizeng + i).ToString());
                    dan = dan.Replace("[guid]", Utils.GetGUID());
                    if (checkBox2.Checked)
                    {
                        dan = dan.Replace(" ", "");
                    }
                    if (checkBox3.Checked)
                    {
                        if (dan.Substring(0, 1) == "[")
                        {
                            dan = "[" + dan.Substring(1, 1).ToUpper() + dan.Substring(2);
                        }
                        else
                        {
                            dan = dan.Substring(0, 1).ToUpper() + dan.Substring(1);
                        }
                    }
                    
                    jieguo.Append(dan);
                    dan = "";
                }
                
                textBox3.Text = jieguo.ToString();
            }
            catch (Exception ex)
            {
                MessageBox.Show("信息输入有误：" + ex.Message);
            }
        }
        
        private void button5_Click(object sender, EventArgs e)
        {
            textBox2.Text = "`[val1]`\r\n";
        }

        private void button2_Click(object sender, EventArgs e)
        {
            textBox2.Text = "[[val1]]\r\n";
        }

        private void comboBox1_SelectionChangeCommitted(object sender, EventArgs e)
        {
            //Clipboard.SetDataObject("[val"+ comboBox1.SelectedItem.ToString()+ "]");
            //textBox2.Focus();

            string val = "[val" + comboBox1.SelectedItem.ToString() + "]";
            string zhi = textBox2.Text;
            int idx = textBox2.SelectionStart;
            zhi = zhi.Insert(idx, val);

            textBox2.Text = zhi;
            textBox2.SelectionStart = idx + val.Count();
            textBox2.Focus();

        }

        private void button6_Click(object sender, EventArgs e)
        {
            shengcheng();
        }

        public void shengcheng() {

            try
            {
                comboBox1.Items.Clear();
                if (string.IsNullOrEmpty(textBox1.Text))
                {
                    MessageBox.Show("不能为空！");
                    return;
                }
                int number = int.Parse(textBox1.Text);
                if (number == 0)
                {
                    MessageBox.Show("不能为0！");
                    return;
                }

                panel1.Controls.Clear();

                int Top = 0;
                for (int i = 1; i <= number; i++)
                {
                    Top = (i - 1) * 130;

                    Label label = new Label();
                    label.Name = "mylabel" + i;
                    label.Top = Top + 55;
                    label.Text = i.ToString() + "";
                    label.Left = 10;

                    TextBox TextBox = new TextBox();
                    TextBox.Multiline = true;
                    TextBox.ScrollBars = ScrollBars.Vertical;
                    Size size = new Size();
                    size.Width = 750;
                    size.Height = 120;
                    TextBox.Size = size;
                    TextBox.MaxLength = 327670;
                    TextBox.DoubleClick += new EventHandler(button_MouseClick);

                    TextBox.Name = i.ToString();
                    TextBox.Top = Top;
                    TextBox.Left = 40;
                    panel1.Controls.Add(TextBox);
                    panel1.Controls.Add(label);
                    comboBox1.Items.Add(i);
                }


            }
            catch (Exception ex)
            {
                MessageBox.Show("信息输入有误：" + ex.Message);
            }

        }
        private void button_MouseClick(object sender, EventArgs e)
        {
            TextBox button = (TextBox)sender;
            button.Focus();
            button.SelectAll();
        }
        private void button3_Click_1(object sender, EventArgs e)
        {
            string val = "[zint]";
            string zhi = textBox2.Text;
            int idx = textBox2.SelectionStart;
            zhi = zhi.Insert(idx, val);

            textBox2.Text = zhi;
            textBox2.SelectionStart = idx + val.Count();
            textBox2.Focus();

            //Clipboard.SetDataObject("[zint]");
            //textBox2.Focus();
        }

        private void DD_Load(object sender, EventArgs e)
        {

            textBox1.Text = "2";
            shengcheng();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            string val = "[guid]";
            string zhi = textBox2.Text;
            int idx = textBox2.SelectionStart;
            zhi = zhi.Insert(idx, val);

            textBox2.Text = zhi;
            textBox2.SelectionStart = idx + val.Count();
            textBox2.Focus();
        }

        private void button7_Click(object sender, EventArgs e)
        {
            string lujing = ShowSaveFileDialog();
            if (string.IsNullOrEmpty(lujing)) { return; }
            string str = textBox3.Text;


            if (checkBox4.Checked)
            {
                StreamWriter sw = new StreamWriter(lujing, false, Encoding.UTF8);
                sw.WriteLine(str);
                sw.Close();//写入
            }
            else
            {
                StreamWriter sw = new StreamWriter(lujing, false, Encoding.Default);
                sw.WriteLine(str);
                sw.Close();//写入
            }
        }

        private string ShowSaveFileDialog()
        {
            string localFilePath = "";
            //string localFilePath, fileNameExt, newFileName, FilePath; 
            SaveFileDialog sfd = new SaveFileDialog();
            //设置文件类型 
            sfd.Filter = "文本（*.txt）|*.txt|Sql文件（*.sql）|*.sql";
            sfd.FileName = "迭代结果";
            //设置默认文件类型显示顺序 
            sfd.FilterIndex = 1;

            //保存对话框是否记忆上次打开的目录 
            sfd.RestoreDirectory = true;

            //点了保存按钮进入 
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                localFilePath = sfd.FileName.ToString(); //获得文件路径 
                string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1); //获取文件名，不带路径

                //获取文件路径，不带文件名 
                //FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("\\")); 

                //给文件名前加上时间 
                //newFileName = DateTime.Now.ToString("yyyyMMdd") + fileNameExt; 

                //在文件名里加字符 
                //saveFileDialog1.FileName.Insert(1,"dameng"); 

                //System.IO.FileStream fs = (System.IO.FileStream)sfd.OpenFile();//输出文件 

                ////fs输出带文字或图片的文件，就看需求了 
            }

            return localFilePath;
        }

        private void textBox5_Click(object sender, EventArgs e)
        {
            DialogResult dr = folderBrowserDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                textBox5.Text = folderBrowserDialog1.SelectedPath;
            }
        }

        private void button8_Click(object sender, EventArgs e)
        {
            string houzhuei = textBox4.Text;
            string mulu = textBox5.Text;
            if (string.IsNullOrEmpty(mulu)) { MessageBox.Show("请选择目录。"); return; }

            try
            {
                #region 获取第一个集合的数量
                int ListNumber = 0;
                foreach (Control item in panel1.Controls)
                {
                    if (item is TextBox && item.Name == "1")
                    {
                        ListNumber = GetList(item.Text).Count();
                        break;
                    }
                }
                #endregion

                string errom = "";
                if (Yanzheng(ListNumber, ref errom))
                {
                    MessageBox.Show("集合数量不统一：\r\n" + errom);
                    return;
                }

                int number = int.Parse(textBox1.Text);
                string yuensuan = textBox2.Text;
                int zizeng = int.Parse(textBox7.Text);

                List<string> jieguo = new List<string>();
                if (yuensuan == "" || number == 0)
                {
                    return;
                }

                string dan = "";

                for (int i = 0; i < ListNumber; i++)
                {
                    dan = yuensuan;
                    foreach (Control item in panel1.Controls)
                    {
                        if (item is TextBox)
                        {
                            TextBox TextBox = (TextBox)item;

                            List<string> list1 = GetList(item.Text);

                            dan = dan.Replace("[val" + item.Name + "]", list1[i]);

                        }
                    }

                    dan = dan.Replace("[zint]", (zizeng + i).ToString());
                    dan = dan.Replace("[guid]", Utils.GetGUID());
                    if (checkBox2.Checked)
                    {
                        dan = dan.Replace(" ", "");
                    }
                    if (checkBox3.Checked)
                    {
                        if (dan.Substring(0, 1) == "[")
                        {
                            dan = "[" + dan.Substring(1, 1).ToUpper() + dan.Substring(2);
                        }
                        else
                        {
                            dan = dan.Substring(0, 1).ToUpper() + dan.Substring(1);
                        }
                    }

                    jieguo.Add(dan);
                    dan = "";
                }
                
                List<string> mingzi = new List<string>();
                foreach (Control item in panel1.Controls)
                {
                    if (item is TextBox && item.Name == "1")
                    {
                        mingzi = GetList(item.Text);
                        break;
                    }
                }

                for (int i = 0; i < mingzi.Count(); i++)
                {
                    string lujing = mulu + "\\" + mingzi[i] + "."+ houzhuei;
                    if (checkBox4.Checked)
                    {
                        StreamWriter sw = new StreamWriter(lujing, false, Encoding.UTF8);
                        sw.WriteLine(jieguo[i]);
                        sw.Close();//写入
                    }
                    else
                    {
                        StreamWriter sw = new StreamWriter(lujing, false, Encoding.Default);
                        sw.WriteLine(jieguo[i]);
                        sw.Close();//写入
                    }
                }
                MessageBox.Show("成功！");
            }
            catch (Exception ex)
            {
                MessageBox.Show("信息输入有误：" + ex.Message);
            }

        }

        private void textBox3_DoubleClick(object sender, EventArgs e)
        {
            textBox3.Focus();
            textBox3.SelectAll();
        }

        private void textBox2_DoubleClick(object sender, EventArgs e)
        {
            textBox2.Focus();
            textBox2.SelectAll();
        }

        private void textBox1_Click(object sender, EventArgs e)
        {
        }
    }
}
